package com.lyw.mine.vm

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import com.lu.library.base.BaseViewModel
import com.lu.library.base.IUiIntent
import com.lu.library.base.IUiState
import com.lyw.login_api.bean.UserInfo
import com.lyw.mine.UserInfoManager
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

sealed class MineUiState: IUiState {
    data object INIT : MineUiState()
    data class UpdateUserInfo(var name:String,var head:String):MineUiState()
}
class MineUiIntent:IUiIntent{

}


@HiltViewModel
class MineVm @Inject constructor(): BaseViewModel<MineUiState, MineUiIntent>(){



    var userInfoObserver:Observer<UserInfo> = object: Observer<UserInfo> {
        override fun onChanged(value: UserInfo) {
            updateUiState {
                MineUiState.UpdateUserInfo(value.name,value.headUrl?:"")
            }
        }

    }
    init {
        UserInfoManager.userInfo.observeForever(userInfoObserver)
    }

    override fun onCleared() {
        super.onCleared()
        UserInfoManager.userInfo.removeObserver(userInfoObserver)
    }
    override fun initUiState(): MineUiState {
        return MineUiState.INIT
    }

    override fun handleIntent(intent: MineUiIntent) {
    }
}